package com.monlyu.action.system;

import java.io.File;

import org.apache.commons.io.FileUtils;

import com.monlyu.action.BaseAction;
import com.monlyu.util.FileUtil;
import com.monlyu.util.LogUtil;
import com.monlyu.util.WebOptUtil;

/********************************************************************************
 * 
 * <b>功能描述☞</b>KindEditor文件上传,包括文件和图片<br>
 * 
 * <b>开发公司☞</b>重庆唯尼科技有限公司<br>
 * <b>版权☞</b>本文件归重庆唯尼科技有限公司所有<br>
 * <b>项目名☞</b>NewsSys<br>
 * <b>文件路径☞</b>com.monlyu.action.system.KEFileLoadAction.java<br>
 * <b>开发日期☞</b>2010-2-25 <br>
 * 
 * @author monlyu
 * @since JDK 5
 * @version 1.0.8
 * 
 ********************************************************************************/
public class KEFileLoadAction extends BaseAction {

	private File imgFile;// 上传的文件
	private String imgFileFileName;// 上传的文件名称

	private String imgWidth;
	private String imgHeight;
	private String align;
	private String imgTitle;
	private String imgBorder;
	private String id;

	private String img;

	public String getImg() {
		return img;
	}

	public void setImg(String img) {
		this.img = img;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getImgWidth() {
		return imgWidth;
	}

	public void setImgWidth(String imgWidth) {
		this.imgWidth = imgWidth;
	}

	public String getImgHeight() {
		return imgHeight;
	}

	public void setImgHeight(String imgHeight) {
		this.imgHeight = imgHeight;
	}

	public String getAlign() {
		return align;
	}

	public void setAlign(String align) {
		this.align = align;
	}

	public String getImgTitle() {
		return imgTitle;
	}

	public void setImgTitle(String imgTitle) {
		this.imgTitle = imgTitle;
	}

	public String getImgBorder() {
		return imgBorder;
	}

	public void setImgBorder(String imgBorder) {
		this.imgBorder = imgBorder;
	}

	public File getImgFile() {
		return imgFile;
	}

	public void setImgFile(File imgFile) {
		this.imgFile = imgFile;
	}

	public String getImgFileFileName() {
		return imgFileFileName;
	}

	public void setImgFileFileName(String imgFileFileName) {
		this.imgFileFileName = imgFileFileName;
	}

	@Override
	public String execute() throws Exception {
		if (checkEnd(imgFileFileName)) {
			File toDir = new File(WebOptUtil.APP_ARGS.getWebFileDir() + WebOptUtil.APP_ARGS.getKeImgDir());
			if (!toDir.exists()) {
				toDir.mkdirs();
			}
			String newName = FileUtil.randFName(imgFileFileName);
			FileUtils.copyFile(imgFile, new File(toDir, newName));
			return ajax(insert(newName));
		}
		return ajax(alert("非法的文件格式,请上传图片"));
	}

	private boolean checkEnd(String name) {
		int at = getImgFileFileName().lastIndexOf(".");
		if (at >= 0) {
			String fileEnd = imgFileFileName.substring(at);
			if (fileEnd.equals(".gif") || fileEnd.equals(".jpg") || fileEnd.equals(".jpeg") || fileEnd.equals(".png")
					|| fileEnd.equals(".bmp")) {
				return true;
			}
		}
		return false;
	}

	private String insert(String name) {
		StringBuilder builder = new StringBuilder();
		String url = WebOptUtil.APP_ARGS.getWebRelUrl() + WebOptUtil.APP_ARGS.getKeImgDir() + "/" + name;
		builder.append("<html>");
		builder.append("<head>");
		builder.append("<meta http-equiv='content-type' content='text/html; charset=utf-8'>");
		builder.append("</head>");
		builder.append("<body>");
		builder.append("<script type='text/javascript'>");
		builder.append("parent.parent.KE.plugin['image'].insert('" + id + "', '" + url + "','" + imgTitle + "','"
				+ imgWidth + "','" + imgHeight + "','" + imgBorder + "','" + align + "');");
		builder.append("</script>");
		builder.append("</body>");
		builder.append("</html>");
		return builder.toString();
	}

	private String alert(String msg) {
		return "<script>alert('" + msg + "')</script>";
	}

	public String deltet() {
		if (img != null) {
			String imgName = img.substring(img.lastIndexOf("/") + 1);
			File file = new File(WebOptUtil.APP_ARGS.getWebFileDir() + WebOptUtil.APP_ARGS.getKeImgDir() + "/"
					+ imgName);
			FileUtils.deleteQuietly(file);
			LogUtil.log(getClass()).info("KindEditor:删除图片:" + imgName);
		}
		return ajax();
	}
}
